from typing import List, Dict
from .BaseInternetSearcher import BaseInternetSearcher
from Logger import Logger
from ConfigManager import ConfigManager
from utils.HashUtils import HashUtils
import requests

logger = Logger.get_logger(__name__)

class WebSearcher(BaseInternetSearcher):
    """Class for handling Internet document searches using Web API"""
    
    def __init__(self):
        """Initialize Web search client"""
        self.api_url = ConfigManager().get('web_search_api') or 'http://localhost:8089/api/search'
        self.timeout = int(ConfigManager().get('web_search_timeout') or 100)

    def search_by_keyword(self, text: List[str], top_k: int = 50) -> List[Dict]:
        """
        Search documents using text input with Web API
        Args:
            text: Input text or list of texts to search with
            top_k: Number of results to return per query
        Returns:
            List of matching documents with title and content
        """
        results = []
        for query in text:
            try:
                # Search using Web API
                response = requests.post(
                    self.api_url.rstrip('/')+'/api/search',
                    json={"keyword": query},
                    timeout=self.timeout
                )
                
                if response.status_code == 200:
                    search_results = response.json()
                    
                    # Process and format results
                    for result in search_results:
                        doc = {
                            'id': HashUtils.get_content_hash(result['href']),
                            'title': result['title'],
                            'text_content': result['abstract'],
                            'url': result['href'],
                            'source': 'web_search'
                        }
                        results.append(doc)
                else:
                    logger.error(f"Web search API returned status code {response.status_code} for query '{query}'")
                    
            except Exception as e:
                logger.error(f"Web search failed for query '{query}': {str(e)}")
                continue
                
        # Remove duplicates based on id
        unique_results = {doc['id']: doc for doc in results}.values()
        return list(unique_results)
